package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.SightTheme;
import com.yoyo.itravel.constants.MsgConstant;
import com.yoyo.itravel.dao.ISightThemeDao;
import com.yoyo.itravel.exceptions.SightThemeException;
import com.yoyo.itravel.service.ISightThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description 景点主题操作服务层接口实现类
 *
 * @author HZX
 * @date 2020/5/9 13:29
 */
@Service
public class SightThemeServiceImpl implements ISightThemeService {

    @Autowired
    private ISightThemeDao sightThemeDao;

    /**
     * @param theme
     * @return boolean
     * @Description 新增景点主题
     */
    @Override
    public boolean addSightTheme(SightTheme theme) throws SightThemeException {
        // 判断sight对象是否为null
        if (theme == null) {
            throw new SightThemeException("传入的景点主题对象为null...");
        }

        try {
            /** 1.判断该景点主题名称是否已经存在 **/
            if (sightThemeDao.selectByName(theme.getThemeName()) != null) {
                // 景点已存在，不可新增景点
                return false;
            }

            /** 2.新增景点 **/
            return sightThemeDao.insert(theme) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightThemeException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @return java.util.List<cn.hut.travel.bean.SightTheme>
     * @Description 查找所有的景点主题
     */
    @Override
    public List<SightTheme> findAllTheme() throws SightThemeException {
        try {
            return sightThemeDao.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightThemeException(MsgConstant.INTERNAL_ERR);
        }
    }

}
